<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Setting;
use think\Request;

class  Set extends Base
{

    public function varlist(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Setting::order("sort asc")->select(),
        );
        return view("varlist",$data);
    }

    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $data = $request->post();
        if(Setting::insert($data)){
            $this->success("添加变量成功","Set/varlist");
        }else{
            $this->error("添加变量失败","Set/addform");
        }

    }
    //编辑
    public function update(Request $request){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Setting::where(array('id'=>$request->get('id')))->find(),
        );
        return view("update",$data);
    }
    public function edit(Request $request){
        $data = $request->post();
        if(Setting::update($data)){
            $this->success("更新变量成功","Set/varlist");
        }else{
            $this->error("更新变量失败","Set/update");
        }

    }
    //删除
    public function delete(Request $request){
        $user = $this->user_info();
        if(Setting::destroy($request->post('id'))){
            $this->success("删除变量成功","Set/varlist");
        }else{
            $this->error("删除变量失败","Set/varlist");
        }
    }
  //上传文件
  public function upload(Request $request){
    //接收上传的文件
    $file = $this->request->file('file');
    if(!empty($file)){
      //图片存的路径
      $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'set';

      // 移动到框架应用根目录/public/uploads/ 目录下

      $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif,ico'])->rule('uniqid')->move($imgUrl);
      $error = $file->getError();
      //验证文件后缀后大小
      if(!empty($error)){
        dump($error);exit;
      }
      if($info){
        // 成功上传后 获取上传信息

        //获取图片的名字
        $imgName = $info->getFilename();
        //获取图片的路径
        $photo= $imgName;

      }else{
        // 上传失败获取错误信息
        $file->getError();
      }
    }else{
      $photo = '';
    }
    if($photo !== ''){
      return ['code'=>1,'msg'=>'成功','photo'=>$photo];
    }else{
      return ['code'=>404,'msg'=>'失败'];
    }
  }




}
